package com.sxit.tdf.server.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sxit.tdf.server.entity.Dept;
import com.sxit.tdf.server.entity.bo.DeptInfoBo;
import com.sxit.tdf.server.entity.bo.SimpleDeptApproveFileBo;
import com.sxit.tdf.server.entity.vo.DeleteDeptByIdVo;
import com.sxit.tdf.server.entity.vo.InsertDeptVo;
import com.sxit.tdf.server.entity.vo.SiftDeptInfoVo;
import com.sxit.tdf.server.response.R;
import com.sxit.tdf.server.service.IDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 前端控制器
 *
 * @author 橙子高🍊
 * @since 2022-02-05
 */
@RestController
@RequestMapping("/dept")
@Tag(name = "DeptController", description = "部门信息相关接口")
public class DeptController {

  @Autowired private IDeptService deptService;

  @Operation(summary = "获取所有的部门简易信息")
  @GetMapping("/getAllDept")
  @SaCheckLogin
  @SaCheckPermission("DEPT:SELECT")
  public R getAllDept() {
    List<Dept> list = deptService.list();
    return R.ok().put("data", list);
  }

  @Operation(summary = "获取部门的信息")
  @PostMapping("/getDeptList")
  @SaCheckLogin
  @SaCheckPermission("DEPT:SELECT")
  public R getDeptList(@Valid @RequestBody SiftDeptInfoVo siftDeptInfoVo) {
    IPage<DeptInfoBo> deptInfoList = deptService.getDeptInfoList(siftDeptInfoVo);
    List<DeptInfoBo> records = deptInfoList.getRecords();
    long total = deptInfoList.getTotal();
    return R.ok().put("data", records).put("total", total);
  }

  @Operation(summary = "插入部门的数据")
  @PostMapping("/insertOrUpdateDept")
  @SaCheckLogin
  @SaCheckPermission(
      value = {"DEPT:UPDATE", "DEPT:INSERT"},
      mode = SaMode.OR)
  public R insertOrUpdateDept(@Valid @RequestBody InsertDeptVo insertDeptVo) {

    Dept dept = new Dept();

    // Id 不为空则为修改数据 否则为添加数据
    if (insertDeptVo.getId() == null) {
      dept.setDeptName(insertDeptVo.getDeptName());
      dept.setUpdateTime(LocalDateTime.now());
      dept.setCreateTime(LocalDateTime.now());
      boolean save = deptService.save(dept);
      if (save) {
        R.ok("添加成功");
      } else {
        R.error("添加失败");
      }
    } else {
      dept.setId(insertDeptVo.getId());
      dept.setDeptName(insertDeptVo.getDeptName());
      dept.setUpdateTime(LocalDateTime.now());
      boolean update = deptService.updateById(dept);
      if (update) {
        R.ok("修改成功");
      } else {
        R.error("修改失败");
      }
    }

    return R.ok();
  }

  @Operation(summary = "删除数据")
  @DeleteMapping("/deleteDeptById")
  @SaCheckLogin
  @SaCheckPermission("DEPT:DELETE")
  public R deleteById(@Valid @RequestBody DeleteDeptByIdVo deleteDeptByIdVo) {
    boolean delete = deptService.deleteById(deleteDeptByIdVo.getId());
    if (delete) {
      return R.ok("删除成功");
    } else {
      return R.error("删除失败");
    }
  }

  @Operation(summary = "获取系部的审批文件")
  @GetMapping("/getDeptApproveFile")
  public R getDeptApproveFile() {

    List<SimpleDeptApproveFileBo> list = deptService.getDeptApproveFile();

    return R.ok().put("data", list);
  }
}
